//
//  ChatMapViewController.m
//  jungoo
//
//  Created by Vu Tu Cuong on 10/1/12.
//
//

#import "ChatMapViewController.h"
#import "MyAnnotation.h"

@interface ChatMapViewController ()

@end

@implementation ChatMapViewController

@synthesize latLonString = _latLonString;

#pragma mark - Private

- (void)loadLocation
{
    NSArray *latlons = [_latLonString componentsSeparatedByString:@";"];
    
    if ([latlons count] == 2) {
        mapView.showsUserLocation=TRUE;
        mapView.mapType = MKMapTypeStandard;

        MKCoordinateRegion region;
        MKCoordinateSpan span;
        span.latitudeDelta = 0.01;
        span.longitudeDelta = 0.01;
        CLLocationCoordinate2D location;
        
        location.latitude = [[latlons objectAtIndex:0] doubleValue];
        location.longitude = [[latlons objectAtIndex:1] doubleValue];
        region.span = span;
        region.center = location;
        
        [mapView regionThatFits:region];
        
        CLLocationDegrees minLatitude = 90;
        CLLocationDegrees maxLatitude = -90;
        CLLocationDegrees minLongitude = 180;
        CLLocationDegrees maxLongitude = -180;
        
        if (location.latitude > maxLatitude && location.latitude < minLatitude && location.longitude >maxLongitude && location.longitude < minLongitude ) {
            [mapView setRegion:region animated:TRUE];
        }
        
        MyAnnotation *anno = [[MyAnnotation alloc] initWithCoords:location name:@""];
        [mapView addAnnotation:anno];
    }
}

#pragma mark - View Lyce

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setBackButtonImageActiveName:@""
                          inactiveName:@"back-button"
                                target:self
                                action:@selector(back)];
    
    [self setTitle:@"Location"];
    
    [self loadLocation];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
 }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Action

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
